<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    use RemindableTrait;

    /* Removes fields from toJSON() method */

    protected $hidden = array('password', 'remember_token');
    protected $validationErrors = null;
    protected $validationRules = array(
        'user_name' => 'required|min:6',
        'password' => 'required|min:8',
        'repeat_password' => 'same:password',
        'email' => 'required|email'
    );

    /**
     * Validations
     */
    public function validate($data = null) {
        $v = Validator::make($data ? $data : $this->attributes, $this->validationRules);

        if ($v->passes()) {
            $this->validationErrors = null;
            return true;
        } else {
            $this->validationErrors = $v->errors();
            return false;
        }
    }

    public function getValidationErrors() {
        return $this->validationErrors;
    }

    /**
     * Relations
     */
    public function userRelations() {
        return $this->hasMany('UserRelations');
    }

    public function recipes() {
        return $this->hasMany('Recipe');
    }

    public function recipesPending() {
        return $this->hasMany('UserRecipesPending');
    }

    public function recipesFavourites() {
        return $this->hasMany('UserRecipesFavourites');
    }
    
    public function notifications() {
        return $this->hasMany('Notification');
    }

    /**
     * Util
     */

    /**
     * Authentication & Reminders
     */
    public function getAuthIdentifier() {
        return $this->getKey();
    }

    public function getAuthPassword() {
        return $this->password;
    }

    public function getRememberToken() {
        return $this->remember_token;
    }

    public function setRememberToken($value) {
        $this->remember_token = $value;
    }

    public function getRememberTokenName() {
        return "remember_token";
    }

    public function getReminderEmail() {
        return $this->email;
    }

}
